//
//  MenuListViewController.m
//  味·道
//
//  Created by lanou3g on 15/6/30.
//  Copyright (c) 2015年 WMR. All rights reserved.
//
#import "MaocHeader.h"
#import "MenuListViewController.h"
#import "MenuListCollectionViewCell.h"
#import "MenuList.h"
#import "DetailViewController.h"
#import "SearchViewController.h"
@interface MenuListViewController ()
@property(nonatomic,retain)UICollectionView *collectionView;
@property(nonatomic,retain)NSMutableArray *dataSorceArr;
@property(nonatomic,assign)NSInteger p;
@property(nonatomic,retain)UIView *backview;
@property(nonatomic,retain)UIPickerView *pickview;
@end

@implementation MenuListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.p = 1;
    [self requestData];
    [self creatCollectionView];
    [self creatPageButton];
    [self creatSearchButton];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


//创建search的button
-(void)creatSearchButton
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(330, 30, 40, 20);
    button.backgroundColor = [UIColor blueColor];
    [button addTarget:self action:@selector(search:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}
//创建pageControl的button
-(void)creatPageButton
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(270, 30, 50, 20);
    button.backgroundColor = [UIColor redColor];
    [button addTarget:self action:@selector(pageView:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}
//创建集合视图
-(void)creatCollectionView
{
    UICollectionViewFlowLayout *flowLayOut = [[UICollectionViewFlowLayout alloc] init];
    flowLayOut.itemSize = CGSizeMake((SCREEN_WIDTH-10)/2 , (SCREEN_WIDTH-10)/2-30);
    flowLayOut.minimumInteritemSpacing = 10;
    flowLayOut.minimumLineSpacing = 10;
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 60, SCREEN_WIDTH, SCREEN_HEIGHT-60) collectionViewLayout:flowLayOut];
    [self.collectionView registerClass:[MenuListCollectionViewCell class ]forCellWithReuseIdentifier:@"menuList"];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.view addSubview:self.collectionView];
}

//解析数据
-(void)requestData
{
    NSString *str = @"%E6%97%A9%E9%A4%90";
    RequestManager *request  = [[RequestManager alloc] initWithRequestURL:[NSString stringWithFormat:@"http://ibaby.ipadown.com/api/food/food.show.list.php?category=%@&p=%ld&pagesize=20",str,self.p] requestModel:GET requestParameter:nil];
    request.delegate = self;
    [request startRequest];
}


#pragma mark - RequestManageDelegate
-(void)requestDidFinishWithData:(NSData *)data
{
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:nil];
    NSArray *arr = [dict objectForKey:@"results"];
    _dataSorceArr = [[NSMutableArray alloc] initWithCapacity:[arr count]];
    for (NSDictionary *dict2 in arr) {
        MenuList *model = [[MenuList alloc] init];
        [model setValuesForKeysWithDictionary:dict2];
        [_dataSorceArr addObject:model];
    }
    [self.collectionView reloadData];
    
}

-(void)requestError:(NSError *)error
{
    NSLog(@"%@",error);
}


#pragma mark - CollectionViewDelegate
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MenuListCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"menuList" forIndexPath:indexPath];
    MenuList *model = [_dataSorceArr objectAtIndex:indexPath.row];
    [cell setModel:model];
//    cell.layer.borderWidth = 2;
//    cell.layer.borderColor = [UIColor grayColor].CGColor;
    cell.layer.cornerRadius = 5;
    cell.layer.masksToBounds = YES;
    return cell;

}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    DetailViewController *detailVC = [[DetailViewController alloc] init];
    MenuList *model = self.dataSorceArr[indexPath.item];
    detailVC.ID = model.ID;
    [self.rightVC.navigationController pushViewController:detailVC animated:YES];
    [detailVC release];
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [self.dataSorceArr count];
    
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

#pragma mark - pageButton
-(void)pageView:(UIButton *)button
{
//    self.backview = [[UIView alloc] initWithFrame:CGRectMake(0, 60, SCREEN_WIDTH, SCREEN_HEIGHT-60)];
//    _backview.backgroundColor = [UIColor blackColor];
//    _backview.alpha = 0.5;
    _pickview = [[UIPickerView alloc] initWithFrame:CGRectMake(50, 250, 280, 300)];
    _pickview.backgroundColor= [UIColor grayColor];
    _pickview.alpha = 1;
    _pickview.showsSelectionIndicator = YES;
    _pickview.delegate = self;
    _pickview.dataSource = self;
    _pickview.layer.cornerRadius = 5;
    _pickview.layer.borderColor = [UIColor whiteColor].CGColor;
    _pickview.layer.borderWidth = 10;
    [self.view addSubview:_pickview];
    
}

#pragma mark - packViewDelegate
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    [_pickview removeFromSuperview];
//    [self.backview removeFromSuperview];
    self.p = row +1 ;
    [self requestData];
    [self creatCollectionView];
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return 78;
}
-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 60;
}
-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
    return 270;
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [NSString stringWithFormat:@"第%ld页",row+1];
}

-(void)search:(UIButton *)button
{
    SearchViewController *searchVC = [[SearchViewController alloc] init];
    [self.rightVC.navigationController pushViewController:searchVC animated:YES];
    [searchVC release];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
